package com.example.dllo.moondog.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by dllo on 16/1/20.
 *  时间差
 */
public class CalculateTimeDifference {
    //此方法只用来计算UTC格式时间差
    public static String timeDifferenceUTC(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date getDate = null;
        String finishTime = "";
        try {
            getDate = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date currentDate = new Date(System.currentTimeMillis());
        long time = (currentDate.getTime() - getDate.getTime());
        long hour = time / 3600000;
        long min = Math.max(time/60000,0);
        int second = (int) Math.max(time/1000,1);

        if (currentDate == getDate) {
            if (hour == 0) {
                if (min == 0) {
                    finishTime = second + "秒前";
                } else {
                    finishTime = min + "分钟前";
                }
            }else {
                finishTime = hour + "小时前";
            }
        }

        long gt = getDate.getTime() / 86400000;
        long ct = currentDate.getTime() / 86400000;
        int days = (int) (ct - gt);
        if (days == 0) {
            if (hour == 0) {
                if (min == 0) {
                    finishTime = second + "秒前";
                } else {
                    finishTime = min + "分钟前";
                }
            }else {
                finishTime = hour + "小时前";
            }
        } else if (days == 1) {
            finishTime = "1天前";
        } else {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy'年'MM'月'dd'日'");
            finishTime = sd.format(getDate);
        }
        return finishTime;
    }
}
